Ext.namespace('GH.ui', 'GH.ui.forms');

GH.ui.forms.MedicalRecordForm = Ext.extend(Ext.form.FormPanel, {
	// defaults - can be changed from outside
    border:false,
    frame:true,
    labelWidth:80,
    loadUrl: '/api/medical_record_load',
    submitUrl:'/api/medical_record_submit',
    
    constructor:function(config) {
        config = config || {};
        config.listeners = config.listeners || {};
        Ext.applyIf(config.listeners, {
            actioncomplete:function() {
                if(console && console.log) {
                    console.log('actioncomplete:', arguments);
                }
            },
            actionfailed:function() {
                if(console && console.log) {
                    console.log('actionfailed:', arguments);
                }
            }
        });
        GH.ui.forms.MedicalRecordForm.superclass.constructor.call(this, config);
    },
    
    initComponent:function() {
        
        // hard coded - cannot be changed from outsid
        var config = {
             defaultType:'textfield',
             //defaults:{anchor:'100%'},
             monitorValid:true,
             autoScroll:true,
             layout:'fit',
             //buttonAlign:'right'
             items:[{
                name:'medical_record',
                fieldLabel:'My Medical Record',
                xtype:'htmleditor',
                //height:170
             }],
             
             buttons:[{
                 text:'Load',
                 scope:this,
                 handler:this.onLoadClick
             },{
                 text:'Submit',
                 formBind:true,
                 scope:this,
                 handler:this.submit
             }],
        };// eo config object
        
        // apply config
        Ext.apply(this, Ext.apply(this.initialConfig, config));
     
        // call parent
        GH.ui.forms.MedicalRecordForm.superclass.initComponent.apply(this, arguments); 
             
   },  // eo function initComponent
        
    /**
     * Form onRender override
     */
    onRender:function() {
     
            // call parent
    	    GH.ui.forms.MedicalRecordForm.superclass.onRender.apply(this, arguments);
     
            // set wait message target
            this.getForm().waitMsgTarget = this.getEl();
            
            this.onLoadClick();
     
            // loads form after initial layout
            //          this.on('afterlayout', this.onLoadClick, this, {single:true});
     
    },  // eo function onRender
    
    /**
     * Load button click handler
     */
    onLoadClick:function() {
        this.load({
             url:this.loadUrl,
             waitMsg:'Loading...',
             success:this.onLoadSuccess,
             failure:this.onLoadFailure,
             params:{
                cmd:'load',
            	csrfmiddlewaretoken: Ext.get("hidden-csrf").dom.children[0].children[0].value
             }
        })
    }, // any additional load click processing here
    
    
    /**
     * loadSuccess handler
     * @param {Ext.form.BasicForm} form
     * @param {Ext.form.Action} action
     * @private
     */
    onLoadSuccess:function(form, action) {
        Ext.Msg.show({
             title:'Success',
             msg:'Medical record load successfully',
             modal:true,
             icon:Ext.Msg.INFO,
             buttons:Ext.Msg.OK
        });
    }, // eo function onSuccess
 
    /**
     * loadFailure handler
     * @param {Ext.form.BasicForm} form
     * @param {Ext.form.Action} action
     * @private
     */
    onLoadFailure:function(form, action) {
    	if(action.failureType == 'server'){ 
            obj = Ext.util.JSON.decode(action.response.responseText); 
            Ext.Msg.alert('Load Medical Record Failed!', obj.errors.reason); 
        }else{ 
            Ext.Msg.alert('Warning!', 'Authentication Failed. Reason: ' + action.failureType); 
        } 
        form.reset();
    }, // eo function onFailure
    
    
    showError:function(msg, title) {
        title = title || 'Error';
        Ext.Msg.show({
             title:title,
             msg:msg,
             modal:true,
             icon:Ext.Msg.ERROR,
             buttons:Ext.Msg.OK
        });
    }, // eo function showError
    
    
    /**
     * Submits the form. Called after Submit buttons is clicked
     * @private
     */
    submit:function() {
        this.getForm().submit({
             url:this.submitUrl,
             scope:this,
             success:this.onSubmitSuccess,
             failure:this.onSubmitFailure,
             params:{
            	 cmd:'save',
            	 csrfmiddlewaretoken: Ext.get("hidden-csrf").dom.children[0].children[0].value
             },
             waitMsg:'Saving...'
        });
    }, // eo function submit
    
    
    /**
     * loadSuccess handler
     * @param {Ext.form.BasicForm} form
     * @param {Ext.form.Action} action
     * @private
     */
    onSubmitSuccess:function(form, action) {
        Ext.Msg.show({
             title:'Success',
             msg:'Medical record submitted successfully',
             modal:true,
             icon:Ext.Msg.INFO,
             buttons:Ext.Msg.OK
        });
    }, // eo function onSuccess
 
    /**
     * loadFailure handler
     * @param {Ext.form.BasicForm} form
     * @param {Ext.form.Action} action
     * @private
     */
    onSubmitFailure:function(form, action) {
    	if(action.failureType == 'server'){ 
            obj = Ext.util.JSON.decode(action.response.responseText); 
            Ext.Msg.alert('Login Failed!', obj.errors.reason); 
        }else{ 
            Ext.Msg.alert('Warning!', 'Authentication Failed. Reason: ' + action.failureType); 
        } 
        form.reset();
    }, // eo function onFailure
    
    
        
}) 

